//
//  RechargeViewController.swift
//  NDAi
//
//  Created by 丸子 on 2024/7/2.
//

import UIKit
import JXSegmentedView

class RechargeViewController: UIViewController {
    var types:String = "1"
    var page:Int = 1
    var dataSource:[RechargeItemModel] = []
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = UITableView.automaticDimension
        tableView.register(cellWithClass: RechargeItemCell.self)
        tableView.separatorStyle = .none
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.emptyDataSetSource = self
        tableView.emptyDataSetDelegate = self
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(hexString: "#F3F2F7")
        self.view.addSubview(self.tableView)
        self.tableView.snp.makeConstraints { make in
            make.top.equalTo(self.safeTop)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(self.safeBottom)
        }
        requestData()
        
        self.tableView.addHeaderRefresh { [weak self] in
            guard let self = self else { return }
            self.page = 1
            self.dataSource = []
            self.tableView.reloadData()
            self.requestData()
        }
        
        self.tableView.addFooterRefresh { [weak self] in
            guard let self = self else { return }
            self.page += 1
            self.requestData()
        }
    }
    
    func requestData(){
        RequestService.instance.order_list(types: self.types, page: self.page) {[weak self] models in
            guard let self = self else { return }
            if self.dataSource.isEmpty {
                self.tableView.endHeaderRefresh()
                self.dataSource = models
            }else {
                self.tableView.showNoMoreData(isNoMoreData: models.count < 10 ? true : false)
                self.dataSource += models
            }
            self.tableView.reloadData()
        } failureClosure: { error in
            
        }
    }
}

extension RechargeViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let model = self.dataSource[indexPath.item]
        let cell = tableView.dequeueReusableCell(withClass: RechargeItemCell.self, for: indexPath)
        cell.moneyLb.text = "\(self.types == "1" ? "+" : "-")\(model.score ?? 0) 钻石"
        cell.channelLb.text = model.title ?? ""
        cell.timeLb.text = (model.create_time ?? "").convertISODateStringToChineseLocalString()
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 88
    }
}
extension RechargeViewController: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}


class RechargeItemCell: UITableViewCell {
    
    lazy var backView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.allRound(8)
        return view
    }()
    
    lazy var channelLb: UILabel = {
        let label = UILabel()
        label.textColor = Theme.color.text_d1
        label.font = UIFont.pingFangSCRegular(ofSize: 14)
        label.text = "苹果"
        return label
    }()
    
    lazy var timeLb: UILabel = {
        let label = UILabel()
        label.textColor = Theme.color.text_d3
        label.font = UIFont.pingFangSCRegular(ofSize: 14)
        label.text = "2024"
        return label
    }()
    
    lazy var moneyLb: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString: "#FF4747")
        label.font = UIFont.pingFangSCRegular(ofSize: 14)
        label.text = "+100 钻石"
        return label
    }()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        backgroundColor = .clear
        self.contentView.addSubviews([backView])
        backView.addSubviews([channelLb, timeLb, moneyLb])
        
        backView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.leading.trailing.equalToSuperview().inset(10)
            make.bottom.equalToSuperview().offset(10)
        }
        
        channelLb.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.leading.equalTo(14)
        }
        
        timeLb.snp.makeConstraints { make in
            make.top.equalTo(channelLb.snp.bottom).offset(6)
            make.leading.equalTo(14)
        }
        
        moneyLb.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-14)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension RechargeViewController: DZNEmptyDataSetDelegate, DZNEmptyDataSetSource {
    open func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        UIImage(named: "icon_nodata")
    }
    
    open func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        let text = "暂无数据，点击刷新～"
        let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.pingFangSCRegular(ofSize: 15), .foregroundColor: Theme.color.text_d3]
        return NSAttributedString(string: text, attributes: attributes)
    }
    
    func emptyDataSet(_ scrollView: UIScrollView!, didTap view: UIView!) {
        self.requestData()
    }
}
